<template>
  <!-- 筛选区 -->
  <div class="sub-filter" v-if="filterData && !filterLoading">
    <div class="item">
      <div class="head">品牌：</div>
      <div class="body">
        <a
          @click="changeBrand(item.id)"
          :class="{ active: item.id === filterData.brands.selectedBrand }"
          href="javascript:;"
          v-for="item in filterData.brands"
          :key="item.id"
        >
          {{ item.name }}
        </a>
      </div>
    </div>
    <div class="item" v-for="item in filterData.saleProperties" :key="item.id">
      <div class="head">{{ item.name }}：</div>
      <div class="body">
        <a
          @click="changeProp(item, prop.id)"
          :class="{ active: prop.id === item.selectedAttr }"
          href="javascript:;"
          v-for="prop in item.properties"
          :key="prop.id"
        >
          {{ prop.name }}
        </a>
      </div>
    </div>
  </div>
  <div v-else class="sub-filter">
    <XtxSkeleton class="item" width="800px" height="40px" />
    <XtxSkeleton class="item" width="800px" height="40px" />
    <XtxSkeleton class="item" width="600px" height="40px" />
    <XtxSkeleton class="item" width="600px" height="40px" />
    <XtxSkeleton class="item" width="600px" height="40px" />
  </div>
</template>
<script>
import { ref, watch } from 'vue'
import { useRoute } from 'vue-router'
import { findSubCategoryFilter } from '@/api/home'
export default {
  name: 'SubFilter',
  setup(props, { emit }) {
    const filterData = ref({})
    const filterLoading = ref(false)
    const route = useRoute()
    watch(
      () => route.params.id,
      (newVal) => {
        if (newVal && `/category/sub/${newVal}` === route.path) {
          filterLoading.value = true
          findSubCategoryFilter(route.params.id).then((data) => {
            data.result.brands.selectedBrand = null
            data.result.brands.unshift({ id: null, name: '全部' })
            data.result.saleProperties.forEach((item) => {
              item.selectedAttr = null
              item.properties.unshift({ id: null, name: '全部' })
            })
            filterData.value = data.result
            filterLoading.value = false
          })
        }
      },
      { immediate: true }
    )

    // 获取筛选参数
    const getFilterParams = () => {
      const filterParams = {} // { brandId:123,attrs[{ groupName: p.name, propertyName: attr.name },...]}
      const attrs = []
      filterParams.brandId = filterData.value.brands.selectedBrand
      filterData.value.saleProperties.forEach((p) => {
        if (p.selectedAttr) {
          const attr = p.properties.find((attr) => attr.id === p.selectedAttr)
          attrs.push({ groupName: p.name, propertyName: attr.name })
        }
      })
      if (attrs.length) filterParams.attrs = attrs
      return filterParams
    }
    // 选择品牌
    const changeBrand = (id) => {
      if (filterData.value.brands.selectedBrand === id) return
      filterData.value.brands.selectedBrand = id
      emit('fliterChang', getFilterParams())
    }
    //选择属性
    const changeProp = (item, id) => {
      if (item.selectedAttr === id) return
      item.selectedAttr = id
      emit('fliterChang', getFilterParams())
    }

    return { filterData, filterLoading, changeBrand, changeProp }
  }
}
</script>
<style scoped lang="less">
// 筛选区
.sub-filter {
  background: #fff;
  padding: 25px;
  .item {
    display: flex;
    line-height: 40px;
    .head {
      width: 80px;
      color: #999;
    }
    .body {
      flex: 1;
      a {
        margin-right: 36px;
        transition: all 0.3s;
        display: inline-block;
        &.active,
        &:hover {
          color: @xtxColor;
        }
      }
    }
  }
}
.xtx-skeleton {
  padding: 10px 0;
}
</style>
